package com.zc58s.rabbitmqdemo3.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author : fujc-dev@qq.com
 * @created : 2022/8/21 9:59
 */
@Configuration
public class RabbitMQConfig {

    public static final String EXCHANGE_TOPICS_INFORM = "exchange_topics_inform";
    public static final String QUEUE_INFORM_EMAIL = "queue_inform_email";
    public static final String QUEUE_INFORM_SMS = "queue_inform_sms";
    //inform.email
    //inform.email.sms
    public static final String ROUTING_KEY_EMAIL = "inform.#.email.#";
    //inform.sms
    //inform.email.sms
    public static final String ROUTING_KEY_SMS = "inform.#.sms.#";

    //声明交换
    @Bean(EXCHANGE_TOPICS_INFORM)
    public Exchange getExchangeTopicsInform() {
        return ExchangeBuilder.topicExchange(EXCHANGE_TOPICS_INFORM).durable(true).build();
    }

    //声明邮件消息队列
    @Bean(QUEUE_INFORM_EMAIL)
    public Queue getQueueInformEmail() {
        return new Queue(QUEUE_INFORM_EMAIL);
    }

    //声明短信掉线队列
    @Bean(QUEUE_INFORM_SMS)
    public Queue getQueueInformSms() {
        return new Queue(QUEUE_INFORM_SMS);
    }

    //绑定邮件交换机和队列以及指定RoutingKey
    @Bean()
    public Binding getBindQueueInformEmail(@Qualifier(QUEUE_INFORM_EMAIL) Queue queue,
                                           @Qualifier(EXCHANGE_TOPICS_INFORM) Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(ROUTING_KEY_EMAIL).noargs();
    }

    //绑定邮件交换机和队列以及指定RoutingKey
    @Bean()
    public Binding getBindQueueInformSms(@Qualifier(QUEUE_INFORM_SMS) Queue queue,
                                         @Qualifier(EXCHANGE_TOPICS_INFORM) Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(ROUTING_KEY_SMS).noargs();
    }
}
